//头文件
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "cJSON.h"

#define LOCAL_IP  "192.168.10.8"
#define LOCAL_PORT 7777 

#define REMOTE_IP  "192.168.10.8"
#define REMOTE_PORT 6666 

typedef struct employee{
	char login[20];
	char type[20];
	char name[20];
	char meeting[100];
	char content[100];
	char start_time[16];
	char end_time[16];
	char place[100];
	char participants[100];
	char message[100];
}EMP;

char p[1000] = {0};
int choice = 0;

//将员工的日志信息打包发给服务器
char *packJson(EMP *stu){
	memset(p, 0, sizeof(char)*1024);
    cJSON* root = cJSON_CreateObject();

	cJSON* login = cJSON_CreateString(stu->login);
	cJSON* type = cJSON_CreateString(stu->type);
	cJSON* name = cJSON_CreateString(stu->name);
	cJSON* meeting = cJSON_CreateString(stu->meeting); 
	cJSON* content = cJSON_CreateString(stu->content); 
	cJSON* start_time = cJSON_CreateString(stu->start_time);
	cJSON* end_time = cJSON_CreateString(stu->end_time);
	cJSON* place = cJSON_CreateString(stu->place);
	cJSON* participants = cJSON_CreateString(stu->participants);
	cJSON* message = cJSON_CreateString(stu->message);
	
    cJSON_AddItemToObject(root, "login", login);
	cJSON_AddItemToObject(root, "type", type);
	cJSON_AddItemToObject(root, "name", name);
	cJSON_AddItemToObject(root, "meeting", meeting);
	cJSON_AddItemToObject(root, "content", content);
	cJSON_AddItemToObject(root, "start_time", start_time);
	cJSON_AddItemToObject(root, "end_time", end_time);
	cJSON_AddItemToObject(root, "place", place);
	cJSON_AddItemToObject(root, "participants", participants);
	cJSON_AddItemToObject(root, "message", message);

    strcpy(p, cJSON_Print(root));
    cJSON_Delete(root);
	return p;
}

void main_menu( ){
	printf("\n登录页面\n");
	printf("1.员工\n");
	printf("2.主管\n");
	printf("3.退出\n");
}

void employee_menu( ){
	printf("\n日志文件管理页面\n");
	printf("1.添加新的日志信息\n");
	printf("2.删除某一条日志信\n");
	printf("3.修改日志信息\n");
	printf("4.查看某一条日志信息\n");
	printf("5.返回\n");
}

void change_menu(){
	printf("1.修改会议名称\n");
	printf("2.修改会议内容\n");
	printf("3.修改开始时间\n");
	printf("4.修改结束时间\n");
	printf("5.修改会议地点\n");
	printf("6.修改参与人员\n");
	printf("7.返回\n");
}

void check_menu( ){
	printf("1.查看员工日志信息\n");
	printf("2.查看主管日志信息\n");
	printf("3.返回\n");
}

void message_menu( ){
	printf("1.同意会议内容");
	printf("2.修改会议内容");
	printf("3.返回\n");
}

int main(int argc,const char* argv[]){
	int sc = socket(AF_INET,SOCK_STREAM,0);
	if(sc == -1)
	{
		perror("socket");
		return -1;
	}
	
	struct sockaddr_in saddr;
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(REMOTE_PORT);
	saddr.sin_addr.s_addr = inet_addr(REMOTE_IP);
	
	int iscon = connect(sc,(struct sockaddr*)&saddr,sizeof(saddr));
	if (iscon == -1)
	{
		perror("connect");
		exit(1);
	}
	char recv_buf[1000]	 = {0};
	EMP stu;
	
	while(1){
		system("clear");
		main_menu( );//员工  主管
		scanf("%d",&choice);
		if(choice == 3){
			break;
		}
		switch(choice){
			case 1://包含员工的增 删 改 查
				memset(&stu,0x0,sizeof(EMP));
				strcpy(stu.login, "employee");
				packJson(&stu);
				write(sc,p,strlen(p));
				while(1){
					system("clear");
					employee_menu( );//增加  删除  修改  查看
					scanf("%d",&choice);
					if(choice == 5){
						break;
					}
					switch(choice){
						case 1://标志‘creat’ 新建一个会议信息，并且将‘待审批’放到message中
							memset(&stu,0x0,sizeof(EMP));
							strcpy(stu.type,"creat");
							strcpy(stu.message, "待审批");
							printf("输入会议信息:\n");
							scanf("%s%s%s%s%s%s%s",stu.name,stu.meeting,stu.content,stu.start_time,stu.end_time,stu.place,stu.participants);
							packJson(&stu);
							write(sc,p,strlen(p));
							break;
						case 2://标志‘delete’ 根据人名和会议名进行删除
							memset(&stu,0x0,sizeof(EMP));
							strcpy(stu.type, "delete");
							printf("输入创建人员名字：\n");
							scanf("%s", stu.name);
							printf("输入会议名称：\n");
							scanf("%s", stu.meeting);
							packJson(&stu);
							write(sc,p,strlen(p));
							break;
						case 3://先根据人名和会议名，在服务器整个删除，然后再重新写入信息
								memset(&stu,0x0,sizeof(EMP));
								stpcpy(stu.type, "change");
								printf("输入要修改人员名字：\n");
								scanf("%s", stu.name);
								printf("输入要修改会议名称：\n");
								scanf("%s", stu.meeting);
								packJson(&stu);
								write(sc,p,strlen(p));
							
								memset(&stu,0x0,sizeof(EMP));
								strcpy(stu.type,"creat");
								strcpy(stu.message, "待审批");
								printf("输入新的会议信息:\n");
								scanf("%s%s%s%s%s%s%s",stu.name,stu.meeting,stu.content,stu.start_time,stu.end_time,stu.place,stu.participants);
								packJson(&stu);
								write(sc,p,strlen(p));
							break;	
						case 4://先根据人名和会议名在服务器进行查询，然后在通过服务器传回结果字符串
							memset(&stu,0x0,sizeof(EMP));
							strcpy(stu.type, "check");
							printf("输入创建人员名字：\n");
							scanf("%s", stu.name);
							packJson(&stu);
							write(sc,p,strlen(p));
							//服务器发一个数据库的结果集通过字符串发过来，客户端接收然后打印
							char *reply2 = NULL;
							reply2 = (char*)malloc(1000);
							memset(reply2,0,1000);
							read(sc,reply2,1000);
							printf("%s\n",reply2);						
							break;
					}
				}
				break;
			case 2:
				memset(&stu,0x0,sizeof(EMP));
				strcpy(stu.login, "supervisor");
				packJson(&stu);
				sendto(sc,p, strlen(p), 0, (struct sockaddr*)&saddr, sizeof(saddr));
				while(1){
					system("clear");
					employee_menu( );//增加 删除 修改 查看
					scanf("%d",&choice);
					if(choice == 5){
						break;
					}
					switch(choice){
						case 1:
							memset(&stu,0x0,sizeof(EMP));
							strcpy(stu.type,"creat");
							printf("输入会议信息:\n");
							scanf("%s%s%s%s%s%s",stu.meeting,stu.content,stu.start_time,stu.end_time,stu.place,stu.participants);
							packJson(&stu);
							printf("%s\n", p);
							write(sc,p,strlen(p));
							break;
						
						case 2:
							memset(&stu,0x0,sizeof(EMP));
							strcpy(stu.type, "delete");
							printf("输入会议名称：\n");
							scanf("%s", stu.meeting);
							packJson(&stu);
							write(sc,p,strlen(p));
							break;
						case 3:							
							memset(&stu,0x0,sizeof(EMP));
							stpcpy(stu.type, "change");
							printf("输入要修改人员名字：\n");
							scanf("%s", stu.name);
							printf("输入要修改会议名称：\n");
							scanf("%s", stu.meeting);
							packJson(&stu);
							write(sc,p,strlen(p));
							
							strcpy(stu.type,"creat");
							memset(&stu,0x0,sizeof(EMP));
							strcpy(stu.type,"creat");
							printf("输入新的会议信息:\n");
							scanf("%s%s%s%s%s%s%s",stu.name,stu.meeting,stu.content,stu.start_time,stu.end_time,stu.place,stu.participants);
							packJson(&stu);
							write(sc,p,strlen(p));
							break;
						case 4://查看主管自己和其下属员工，可以通过选择进行同意或拒绝下属员工的会议
							while(1){
								system("clear");
								check_menu( );//查看未审批员工的   查看主管自己的
								scanf("%d",&choice);
								if(choice == 3){
									break;
								}
								switch(choice){
									case 1:
										memset(&stu,0x0,sizeof(EMP));
										strcpy(stu.login, "approval");
										packJson(&stu);
										write(sc,p,strlen(p));
										//服务器发一个数据库的结果集包过来，客户端接收然后打印
										char *reply2 = NULL;
										reply2 = (char*)malloc(1000);
										memset(reply2,0,1000);
										read(sc,reply2,1000);
										printf("%s\n",reply2);
										
										while(1){
											system("clear");
											message_menu( );//同意-标志‘agree’  不同意-标志‘disagree’
											scanf("%d",&choice);
											if(choice == 3){
												break;
											}
											switch(choice){
												case 1:
													memset(&stu,0x0,sizeof(EMP));
													strcpy(stu.message, "agree");
													printf("输入要同意审核人员名字：\n");
													scanf("%s", stu.name);
													printf("输入要同意审核会议名称：\n");
													scanf("%s", stu.meeting);
													packJson(&stu);
													write(sc,p,strlen(p));
													break;
												case 2:
													memset(&stu,0x0,sizeof(EMP));
													strcpy(stu.message, "disagree");
													printf("输入要拒绝审核人员名字：\n");
													scanf("%s", stu.name);
													printf("输入要拒绝审核会议名称：\n");
													scanf("%s", stu.meeting);
													packJson(&stu);
													write(sc,p,strlen(p));								
													break;
											}
										}
										break;
									case 2:
										memset(&stu,0x0,sizeof(EMP));
										strcpy(stu.type, "check");
										printf("输入人员名字：\n");
										scanf("%s", stu.name);
										packJson(&stu);
										write(sc,p,strlen(p));
										//服务器发一个数据库的结果集包过来，客户端接收然后打印
										char *reply = NULL;
										reply = (char*)malloc(1000);
										memset(reply,0,1000);
										read(sc,reply,1000);
										printf("%s\n",reply);
										break;
								}
							}
							break;
					}
				}
				break;
		}
	}
	
	close(sc);
	return 0;
}